﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agile.Engine.Xml
{
    public class DataProviderResource : XmlResource
    {

        /// <summary>
        /// 解析资源并填充到缓存
        /// </summary>
        /// <param name="stream"></param>
        protected override void PaserResource(System.IO.Stream stream)
        {

            System.Xml.Linq.XElement root = System.Xml.Linq.XElement.Load(stream);

            foreach (var el in root.Elements("data").AsParallel())
            {
                this[el.Attribute("key").Value] = new Tuple<string, string>(el.Attribute("type").Value, el.Attribute("value").Value);
            }
        }

        public Spring.Data.Core.AdoTemplate CreateTemplate(string key)
        {
            var provider = CreateDataProvider(key);
            return new Spring.Data.Core.AdoTemplate(provider);
        }

        public Spring.Data.Common.IDbProvider CreateDataProvider(string key)
        {
            var tuple = this[key] as Tuple<string, string>;
            var provider = Spring.Data.Common.DbProviderFactory.GetDbProvider(tuple.Item1);
            provider.ConnectionString = Agile.Engine.Utils.VelocityUtils.MergeString(tuple.Item2, ObjectContext.Environment.Registry);
            return provider;
        }

    }
}
